import os
from shutil import copyfile


# 源文件路径
origin_path = "E:/Virtual/gitee/voice-origin/"
# mp3输出路径
mp3_path = "E:/Virtual/gitee/voice-mp3/"
# ffmpeg的路径
ffmpeg_info = "E:/MyTools/ffmpeg-4.3.1-2020-11-02-full_build/bin/ffmpeg -i"


def change_mp3():
    dir_list = os.listdir(origin_path)
    for dir_name in dir_list:
        current_path = origin_path + dir_name
        current_mp3_path = mp3_path + dir_name
        if not os.path.exists(current_mp3_path):
            os.mkdir(current_mp3_path)
        file_list = os.listdir(origin_path + dir_name)
        for file_name in file_list:
            current_file = f'{current_path}/{file_name}'
            current_mp3_file = f'{current_mp3_path}/{file_name[0:-4]}.mp3'
            if file_name[-3:-1] != "mp3":
                if not os.path.exists(current_mp3_file):
                    cmd = f'{ffmpeg_info} {current_file} {current_mp3_file}'
                    os.system(cmd)

                try:
                    create_time = os.path.getctime(current_file)
                    # 修改文件的访问时间和修改时间
                    os.utime(current_mp3_file, (create_time, create_time))
                except:
                    print('找不到文件 ' + current_mp3_file)
            else:
                print("不需要转换格式 " + current_mp3_file)
                copyfile(current_file, current_mp3_file)


def main():
    change_mp3()


if __name__ == '__main__':
    main()
